#使用pandas分别从Text、CSV、Excel、Html、MySQL、MongoDB中加载数据
import pandas as pd

# 从text加载数据
def load_from_txt():
    print("从text加载数据")
    data=pd.read_table("data/data1.txt")
    print(data)

# 从csv加载数据
def load_from_csv():
    print("=========从csv加载数据=========")
    data=pd.read_csv("data/data2.csv")
    print(data)

# 从excel加载数据
def load_from_excel():
    print("=========从excel加载数据=========")
    data=pd.read_excel("data/data3.xlsx")
    print(data)

# 从html加载数据
def load_from_html():
    print("=========从html加载数据========")
    tbs=pd.read_html("data/data4.html",header=0,attrs={"class":"mytable"})
    print(tbs[0])

# 从mysql加载数据
def load_from_mysql():
    print("\n=========从mysql加载数据=========\n")
    try:
        import pymysql
        conn = pymysql.connect(host="localhost",user="root",password="123123",db="school",charset="utf8")
        sql="select * from book"
        data=pd.read_sql(sql,conn)
        conn.close()
        print(data)
    except Exception as err:
        print("出错：请检查数据库配置！")

# 从mongodb加载数据
def load_from_mongo():
    print("\n=========从mongodb加载数据=========\n")
    try:
        import pymongo
        client=pymongo.MongoClient('127.0.0.1',27017)
        db="school"
        table="student"
        cursor=client[db][table].find()
        data=pd.DataFrame(list(cursor))
        print(data)
    except Exception as err:
        print("出错：请检查数据库配置！")

if __name__=="__main__":
    load_from_txt()
    load_from_csv()
    load_from_excel()
    load_from_html()
    load_from_mysql()
    load_from_mongo()



